#singleinstance,force
fileinstall,commands.ini,commands.inidelete,1
fileinstall,scilexer.dll,scilexer.dll,1
detecthiddenwindows,on
coordmode,mouse,relative
ifnotexist scilexer.dll
{
 splashtexton,300,50,Downloading support files,Please wait
 urldownloadtofile,http://www.maestrith.com/files/SciLexer.dll,scilexer.dll
}
ifnotexist commands.ini
urldownloadtofile,http://www.maestrith.com/files/commands.ini,commands.ini
splashtextoff
gui()
#include *i plugins.ini
hotkey(1,gui("options"))
return
gh:
if isfunc(a_guicontrol)
%a_guicontrol%()
return
guisize:
settimer,size,400,on
onmessage(0x4E,"")
size("main")
return
2guisize:
size("segments")
return
3guisize:
size("variables")
return
4guisize:
size("ideas")
return
size:
if !getkeystate("lbutton","P")
{
 OnMessage(0x4E, "OnWmNotify")
 settimer,size,off
}
return
menu:
mm()
return
3guiclose:
2guiclose:
4guiclose:
guiclose()
return
options:
if a_guievent=Normal
controlfocus,msctls_hotkey321,Settings
return
7guiescape:
7guiclose:
hotkey(1,gui("options"))
gui,7:destroy
return
indent:
iniwrite(window(7,"indent"),"indent","level")
return
9guiescape:
gui,9:destroy
return
guidropfiles:
drop()
12guiclose:
12guiescape:
keywords(98)
set(gui(1))
gui,12:destroy
return 
keymap:
keymap(a_thishotkey)
return
find:
find()
return
remrep:
lv_delete(lv_getnext())
return
10guiescape:
10guiclose:
gui,10:submit,nohide
filedelete,% currentproject(1) "\version.ini"
fileappend,% stringreplace(window(10,"info"),window(10,"version")),% currentproject(1) "\version.ini"
gui,10:destroy
return
gui,10:destroy
return
getpercentage:
getper()
return
77guiescape:
gui,77:destroy
return
88guiescape:
gui,88:destroy
return
ghandler:
if isfunc(a_guicontrol)
%a_guicontrol%()
return
rightclickmenu:
test_rcm()
return
rcmhandle:
rcmhandle()
return
uploadexe:
getper(currentproject(1) "\" gui("noext") ".exe")
return
addrep(){
 d(8)
 controlgettext,abbreviation,Edit1,A
 controlgettext,replacement,Edit2,A
 lv_add("",abbreviation,replacement)
 cst("Edit1","","Replacements")
 cst("Edit2","","Replacements")
 controlfocus,Edit1,Replacements
}
add_marker(){
 sc:=gui(1)
 scintilla(sc,2040,5,2)
 scintilla(sc,2043,cline(),5)
}
add_recent(addfile){
 while,x:=IniRead("recent",A_Index,0)
 list.=x "`n"
 stringreplace,list,list,%addfile%`n,,All
 list:=addfile "`n" list
 while,x:=split(list,"`n",A_Index)
 if a_index < 10
 IniWrite("recent",A_Index,x)
}
add_variable(){
 inputbox,out,Add a variable,Variable name
 if !out
 return
 out:=clean1(out)
 d(3)
 loop,% count(3)
 {
  if loop(3,a_index) = out
  {
   msgbox Variable already exists
   return
  }
 }
 lv_add("",out),save()
 inidelete,% currentproject(1) "\options.ini",variables
 loop,% count(3)
 IniWrite,% Loop(3,a_index),% currentproject(1) "\options.ini",variables,%A_Index%
 keywords(98)
}
about(){
 window("9,About,,Edit~w400 h500~,button~vfacebook ggh~Facebook homepage")
 about=
 (
 I will have more here in the future`r`nI would like to thank everyone in the community that is doing something to promote Autohotkey, developing for it, and sharing their code freely so that we can all learn from them.
 License for Scintilla and SciTE 
 Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
 All Rights Reserved:
 Permission to use, copy, modify, and distribute this software and its 
 documentation for any purpose and without fee is hereby granted, 
 provided that the above copyright notice appear in all copies and that 
 both that copyright notice and this permission notice appear in 
 supporting documentation. 
 NEIL HODGSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 
 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 
 AND FITNESS, IN NO EVENT SHALL NEIL HODGSON BE LIABLE FOR ANY 
 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 
 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE 
 OR PERFORMANCE OF THIS SOFTWARE.
 )
 cst("Edit1",about,"About")
}
autoopen(list="",open=""){
 SplashImage,"",MS20,,Opening projects.,Please wait,Tahoma
 while,x:=split(list,"|",A_Index){
  currentproject(x)
  Gui,1:Default
  sc:=gui(1),projects(x)
  if !x
  {
   scintilla(sc,2171,1)
   goto oe
  }
  SplitPath,x,,dir
  WinHide,%A_ScriptName%
  loop,read,%x%
  {
   window:=scintilla(sc,2375)
   scintilla(sc,2358,,window)
   contents:=read(dir "\" substr(a_loopreadline,10))
   sendstring(sc,2181,,contents),scripts(substr(a_loopreadline,10),contents),segment:=substr(a_loopreadline,10),seglist.=segment ",",winnumber(segment,window),set(sc)
   scintilla(sc,2014)
  }
  WinShow,%A_ScriptName%
  loop,parse,seglist,`,
  if (A_LoopField)
  total.=A_LoopField "|"
  oe:
  segkeeper(x,total),getpos("load")
  font(0)
  GuiControl,1:,SysTabControl321,% "|" projects()
  GuiControl,1:,SysTabControl322,|%total%
  total:="",seglist:=""
  SplitPath,x,,dir
  ideaholder(x,Read(dir "\ideas.ini"))
  ControlSetText,Edit1,% ideaholder(x),Ideas
  LV_Delete()
  IniRead,variable,%dir%\options.ini,lastopen,variable
  iniread,variables,%dir%\options.ini,variables
  d(3)
  while,var:=split(variables,"`n",A_Index),add:=split(var,"=",2){
   select:=add=variable?"Select":""
   if select
   lastvariable(x,add)
   lv_add(select,add)
  }
  lv_modifycol(1,"AutoHdr")
  gui,1:default
  RegExReplace(projects(),"\|",0,count)
  IniRead,segment,%dir%\options.ini,lastopen,segment
  if segment=Error
  SplitPath,x,,,,segment
  sv("end",iniread("end","char"," "))
  select(segment,2)
 }
 open:=!open ? IniRead("lastopen","currentproject") : list
 GuiControl,1:ChooseString,SysTabControl321,%open%
 sleep,10
 SetTimer,project,1
 SplashImage,Off
 SplashTextOff
}
auto_update(){
 save()
 splitpath,a_scriptname,,,,filename
 version:=gui("version") ,  ext:=if A_IsCompiled ? ".exe" : ".ahk"
 filemove,%a_scriptname%,backup_%version%_%a_scriptname%,1
 SplashTextOn,500,200,Please Wait,Downloading the new version.  This could take a minute
 urldownloadtofile,http://www.autohotkey.net/~maestrith/Script Writer/Script Writer%ext%,%a_scriptname%
 reload
}
background_color(){
 iniwrite("font","background",Dlg_Color(iniread("font","background",0)))
 dcolors("reload")
 setcolor(gui(1))
 setcolor(edit_colors(1))
}
brace(){
 if !options("&auto fix indentation",1)
 return
 if a_thishotkey in `{,`}
 send,{%a_thishotkey%}
 fix_indent()
}
Character_Count(){
 sc:=gui(1)
 count:=abs(scintilla(sc,2143) - scintilla(sc,2145))
 if !count
 msgbox % strlen(scripts(currentsegment()))
 else
 msgbox %count%
}
check(){
 controlget,sort,checked,,Button1,A
 controlgettext,find,Edit1,A
 iniwrite,%find%,% Currentproject(1) "\options.ini",search,find
 controlgetfocus,out,Search
 if out=Edit1
 {
  GuiControl,5:-Redraw,TV
  d(5)
  tv_delete()
  search(find,sort)
  d(5)
  if tv_getcount() 
  controlfocus,SysTreeView321,Search 
  GuiControl,5:+Redraw,TV
 }
 else
 treeview(find)
}
clean(x){
 return regexreplace(regexreplace(x,"i)(?:[^a-z0-9_]|\&)","")," ","_")
}
cline(){
 controlget,line,currentline,,Scintilla1,%a_scriptname%
 return line-1
}
compile(){
 save()
 msgbox,36,Compile Script,Would you like to compile and upload your script?,0
 IfMsgBox,no
 return
 SplitPath,A_AhkPath,,dir
 compiler=%dir%\compiler\ahk2exe.exe
 IfnotExist,%compiler%
 {
  MsgBox,can not find compiler.  Please install Autohotkey again.
  return
 }
 lastopen:=currentproject()
 SplitPath,lastopen,,,,noext
 SplitPath,lastopen,,updir
 splashtexton,300,50,Compiling Script,Please wait
 RunWait,ahk2exe.exe /in "%a_scriptdir%\ftpfile.ftp" /out "%updir%\%noext%.exe" /mpress 1,%dir%\compiler\
 SplashTextOff
 server:=iniread("ftp","server"),user:=iniread("ftp","user"),pass:=iniread("ftp","pass"),uploaddir:=iniread("ftp","directory","",1), filename:=currentproject(),name:=currentproject(3)
 up=mkdir "%uploaddir%"
 up:=!uploaddir?:up
 FileDelete FTPCommands.txt
 FileAppend,
 (
 open %server%`n%user%`n%pass%
 binary
 hash
 %up%
 put "%updir%\%name%.exe" "%uploaddir%/%name%.exe"
 ls -l
 quit
 ), FTPCommands.txt
 settimer,uploadexe,50
 RunWait %comspec% /c ftp.exe -s:"FTPCommands.txt" > "FTPLog.txt",,hide
 FileDelete FTPCommands.txt
 loop,read,FTPLog.txt
 {
  regexmatch(a_loopreadline,"You uploaded(.*)",out)
  if out
  {
   settimer,uploadexe,off
   progress,off
   msgbox %out%
   break
  }
 }
 return
}
copy(){
 send,^c
}
count(x){
 d(x)
 return lv_getcount()
}
create_segment_from_selection(){
 sc:=gui(1)
 text:=Scintilla_GetSelText(sc)
 text:=strget(&text,"",utf-16)
 if !text
 exit
 inputbox,out,New segment name?,Input a new segment name
 ifexist % currentproject(1) "\" out
 {
  msgbox file exists choose another one
  return
 }
 if !out
 return
 send,{bs}
 update()
 sleep,1
 new_segment(out)
 sleep,100
 sendstring(sc,2181,,text)
 set(sc)
}
cst(x,y,z){
 controlsettext,%x%,%y%,%z%
}
cut(){
 send,^x
 update()
 return
 cut:
 update()
 return
}
cv(){
 if (a_guievent="i" || a_guievent="normal")
 sb_settext("Current variable: " get(3,1),3)
}
d(x){
 gui,%x%:default
}
dcolors(x){
 static
 if x=reload
 loadedcolors:=object()
 if !loadedcolors.maxindex()
 {
  loadedcolors:=object(6,255,7,16744576,8,999999,"background",0)
  while,y:=split(iniread("font"),"`n",a_index)
  loadedcolors[split(y,"=",1)]:=split(2)
 } 
 if loadedcolors[x] <> ""
 return loadedcolors[x]
 return 0xffffff
}
debug(){
 debug:
 settimer,debug,Off
 save()
 lastopen:=currentproject(),dir:=currentproject(1),path:=a_ahkpath
 file=%dir%\debug.bat
 dfile=%dir%\debug.dbg
 filedelete,%dfile%
 filedelete,%file%
 fileappend,`"%a_ahkpath%`" /ErrorStdOut `"%lastopen%`" > debug.dbg 2>&1,%file%
 runwait,"%file%",%dir%,hide
 debug:=read(dfile)
 if debug
 {
  file1:=SubStr(debug,1,out:=InStr(debug,"(")-2)
  line:=SubStr(debug,out+3,instr(debug,")")-out-3)
  RegExMatch(debug,"i)Specifically:(.*)",out)
  out:=substr(out,15)
  splitpath,file1,name
  select(name,2)
  tooltip,%debug%
  sc:=gui(1)
  scintilla(sc,2024,line-1)
  scintilla(sc,2190, scintilla(sc,2008))
  scintilla(sc,2192,scintilla(sc,2006))
  start:=sendstring(sc,2197,strlen(out),out,"int")
  Scintilla(sc,2160,start,start+strlen(out))
 }
 return
}
Dlg_Color(Color, setcolor=""){
 VarSetCapacity(CHOOSECOLOR, 0x24, 0), VarSetCapacity(CUSTOM, 64, 0)
 ,NumPut(0x24,CHOOSECOLOR, 0), NumPut(hGui,CHOOSECOLOR, 4)
 ,NumPut(color,CHOOSECOLOR, 12), NumPut(&CUSTOM,CHOOSECOLOR, 16)
 ,NumPut(0x00000103,CHOOSECOLOR, 20)
 nRC := DllCall("comdlg32\ChooseColorA", str, CHOOSECOLOR)
 if (errorlevel <> 0) || (nRC = 0)
 return color
 setformat,integer,H
 clr := NumGet(CHOOSECOLOR, 12)
 setformat,integer,D
 return %clr%
}
Dlg_Font(ByRef Name, ByRef Style, ByRef Color, Effects=true, hGui=0) {
 strput(strget(&name),&name,"CP0")
 LogPixels := DllCall("GetDeviceCaps", "uint", DllCall("GetDC", "uint", hGui), "uint", 90)
 VarSetCapacity(LOGFONT, 128, 0)
 Effects := 0x041 + (Effects ? 0x100 : 0)
 DllCall("RtlMoveMemory", "uint", &LOGFONT+28, "Uint", &Name, "Uint", 32)
 clr:=color
 if InStr(Style, "bold")
 NumPut(700, LOGFONT, 16)
 if InStr(Style, "italic")
 NumPut(255, LOGFONT, 20, 1)
 if InStr(Style, "underline")
 NumPut(1, LOGFONT, 21, 1)
 if InStr(Style, "strikeout")
 NumPut(1, LOGFONT, 22, 1)
 if RegExMatch(Style, "s[1-9][0-9]*", s){
  StringTrimLeft, s, s, 1
  s := -DllCall("MulDiv", "int", s, "int", LogPixels, "int", 72)
  NumPut(s, LOGFONT, 0, "Int")
 }
 else  NumPut(16, LOGFONT, 0)
 VarSetCapacity(CHOOSEFONT, 60, 0)
 ,NumPut(60,CHOOSEFONT,0)
 ,NumPut(hGui,CHOOSEFONT, 4)
 ,NumPut(&LOGFONT,CHOOSEFONT, 12)
 ,NumPut(Effects,CHOOSEFONT, 20)
 ,NumPut(clr,CHOOSEFONT, 24)
 r := DllCall("comdlg32\ChooseFontA", "uint", &CHOOSEFONT)
 if !r
 return false
 VarSetCapacity(Name, 32)
 DllCall("RtlMoveMemory", "str", Name, "Uint", &LOGFONT + 28, "Uint", 32)
 Style := "s" NumGet(CHOOSEFONT, 16) // 10
 old := A_FormatInteger
 SetFormat, integer, hex
 Color := NumGet(CHOOSEFONT, 24)
 SetFormat, integer, %old%
 Style =
 VarSetCapacity(s, 3)
 DllCall("RtlMoveMemory", "str", s, "Uint", &LOGFONT + 20, "Uint", 3)
 if NumGet(LOGFONT, 16) >= 700
 Style .= "bold "
 if NumGet(LOGFONT, 20, "UChar")
 Style .= "italic "
 if NumGet(LOGFONT, 21, "UChar")
 Style .= "underline "
 s := NumGet(LOGFONT, 0, "Int")
 Style .= "s" Abs(DllCall("MulDiv", "int", abs(s), "int", 72, "int", LogPixels))
 name:=strget(&name,"",utf-16)
 iniwrite("font",5,color)
 iniwrite("font","style",style)
 iniwrite("font","name",name)
 return 1
}
download(x,y,z=""){
 file:=if y ? y : temp.tmp
 urldownloadtofile,%x%,%file%
 if z
 return read(y)
}
download_list(x=""){
 static
 d(11)
 lv_delete()
 list:=download("http://www.autohotkey.net/~maestrith/Script Writer/plugins","list.html",1)
 list:=RegExReplace(list,"(%20)"," ")
 loop,parse,list,`n
 {
  search="(.*).ahk"
  regexmatch(a_loopfield,search,out)
  if out1
  lv_add("",out1 ".ahk")
 }
 gui,11:add,button,x+10 vinstall ggh,Download and Install current plugin
}
drop(){
 if a_guievent contains `n
 msgbox one at a time please
 splitpath,a_guievent,file,dir,ext,noext
 msgbox,4,Ahk file dropped,Would you like to make this a new segment?
 ifmsgbox no
 return
 new_segment(noext)
 sleep,100
 text:=read(a_guievent)
 sendstring(gui(1),2181,,text)
}
dwellmessage(sc,off=""){
 mousegetpos,x,y
 y-=52 , x+=4
 pos:=Scintilla(sc,2022,x,y)
 pos:=Scintilla(sc,2166,pos)
 out:=scintilla_gettext(sc)
 if !scintilla(sc,2230,pos)
 {
  while,x:=split(out,"`n",a_index)
  {
   if a_index <= %pos%
   continue
   if !scintilla(sc,2228,a_index)
   total.=x
  }  
  tooltip,% substr(total,1,250) "`n...................."
 }
 if off
 tooltip,
}
Edit_Colors(x=0){
 static fc
 if x
 return fc
 gui,99:destroy
 gui,99:+owner1
 color:=iniread("font","color")
 gui,99:show,w550 h300,Set Colors 
 winget,id,id,Set Colors
 font_color:=Scintilla_Add(id,0,0,550,300)
 menu,colors,add,Background color,menu
 menu,colors,add,Word wrap markers,menu
 menu,colors,add,Line numbers background,menu
 menu,99:bar,add,colors,:colors
 gui,99:menu,colors
 fc:=font_color
 scintilla(font_color,2246,0,1)
 set(font_color)
 out=/*`nMulti-Line`ncomments`n*/`nSelect the text to change the colors`nThis is a sample of normal text`n`"incomplete quote `n"complete quote"`n`;comment`n0123456789`n()[]^&*()+~`{`}@$`%`#!``b``a``c``k``t``i``c``k`n
 loop,6
 out.="Keyword set" a_index "=" keywords(a_index)  "`n"
 out.="Keyword sets 4-6 are yours to add whatever you like. `nGoto Edit/Edit highlight text to add/edit your lists."
 sendstring(font_color,2181,,out)
 setcolor(font_color)
 loop,34
 Scintilla(font_color,2409,a_index-1,1)
 scintilla(font_color,2242,0,sendstring(font_color,2276,33,"_" scintilla(font_color,2154),"int"))
}
edit_highlight_text(){
 window("12,Highlight text editor,,text~w800~All values must be in lower case and space delimited eg(list of words),listview~vkeyword_set altsubmit ggh w200 h400~Keyword Set,Edit~vsetkeywords ggh x+10 w600 h400~") 
 d(12)
 loop,3
 lv_add("","Keyword set " a_index+3)
 lv_modify(1,"select")
}
edit_replacements(){
 Window("8,Replacements,,text~~Enter an abbreviation and replacement here`nYou will need to re-start the program for these replacements to work.,listview~w400 h500~Abbreviation|Replacement,text~~Abbreviation:,edit~x+5 w200 vabbreviation~,text~x10~Replacement,edit~x+5 w320 vreplacement~,button~default ggh vaddrep~Add replacement,button~gremrep x+10~Remove,button~ggh vsaverep x+10~Save and Activate")
 d(8)
 while,x:=split(iniread("replacements"),"`n",a_index)
 lv_add("",split(x,"=",1),split(2))
}
endrep(){
 controlgettext,out,Edit5,A
 while,x:=split(out,"",a_index)
 list.=asc(x) ","
 iniwrite("end","char",list)
 sv("end",list)
}
End_of_line(){
 scintilla(gui(1),2356,iniread("options","end_of_line",0))
}
enter(){
 if !options("&auto fix indentation",1) 
 return
 sc:=gui(1)
 if scintilla(sc,2007,scintilla(sc,2008)) = 32
 send,^{right}
 fix_indent()
}
exit(){
 save(1)
 exitapp
}
export(){
 save()
 fileselectfile,out,S,% currentproject(2),Export Script As....,*.ahk
 if ErrorLevel
 return
 if out not contains .ahk
 out.=".ahk"
 publish(out)
}
export_plugin(){
 save()
 open:=gui("lastopen")
 splitpath,open,file
 if !InStr(read("plugins.ini"),"plugins\" file)
 fileappend,% Chr(35) "include *i plugins\" file "`n",%A_ScriptDir%\plugins.ini
 publish(a_scriptdir "\plugins\" file)
 reload
}
facebook(){
 run,http://www.facebook.com/pages/AHK-Script-Writer/104905522934583
}
find(){
 window("5,Search," gui("search") ",edit~-multi x5 y5~,treeview~ggh altsubmit w300 h390 vtv,checkbox~ggh vsort~Sort by segment,button~default ggh vcheck hidden~Search")
 cst("Edit1",iniread("search","find","Input text here",1),"Search")
 if iniread("search","sort",0,1)
 control,check,,Sort by segment,Search
 controlfocus,Edit1,Search
 send,^A
 return
}
findandreplace(){
 controlgettext,out,Edit1,Replace Text
 controlgettext,out1,Edit2,Replace Text
 d(2)
 sc:=gui(1)
 while,found:=split(segkeeper(),"|",A_Index){
  script:=scripts(found)
  if script contains %out%
  {
   d(2)
   select(found,2)
   sleep,100
   loop,
   {
    scintilla(sc,2190, found)
    scintilla(sc,2192,scintilla(sc,2006)) 
    start:=sendstring(sc,2197,strlen(out),out,"int")
    Scintilla(sc,2160,start,start+strlen(out))
    if start=-1
    {
     found=0
     break
    }
    msgbox,35,Text Found.,Replace this occurence,0
    controlfocus,Scintilla1,%a_scriptname%
    ifmsgbox,yes
    {
     send,%out1%
     update()
    }
    ifmsgbox,cancel
    exit
    found:=scintilla(sc,2008)+1
   }
  }
 }
}
find_in_segment(){
 window("88,Find in Segment,,edit~w400~,radio~vnext checked~Next,radio~vprevious~Previous,button~default ggh vfis~Find")
}
fis(){
 sc:=gui(1)
 controlget,next,checked,,Next
 controlgettext,out,Edit1,Find in Segment
 if !next
 {
  scintilla(sc,2190, scintilla(sc,2008)-1)
  scintilla(sc,2192,scintilla(sc,0))
  start:=sendstring(sc,2197,strlen(out),out,"int")
  if start=-1
  {
   msgbox Reached the begining of the selection.
   return
  }
  Scintilla(sc,2160,start,start+strlen(out))
 }
 else
 {
  scintilla(sc,2190, scintilla(sc,2008))
  scintilla(sc,2192,scintilla(sc,2006))
  start:=sendstring(sc,2197,strlen(out),out,"int")
  if start=-1
  {
   msgbox Reached the begining of the selection.
   return
  }
  Scintilla(sc,2160,start,start+strlen(out))
 }
}
fix_indent(){
 scintilla(gui(1),2029,"CRLF")
 settimer,longwait,500
 ind:=gui("indent"),sc:=gui("sc"),out:=scripts(currentsegment()),indent:=0
 scintilla(sc,2078)
 loop,parse,out,`r,`n
 {
  if a_index=1
  Scintilla(sc,2126,(a_index-1),0)
  out:=split(a_loopfield,";",1)
  loop,parse,out,`r,%a_space%
  out:=A_LoopField
  if SubStr(out,0) = "{"
  indent:=indent + ind
  if out = `}
  {
   indent:=indent - ind
   Scintilla(sc,2126,(a_index-1),indent)
  }
  if a_index = % scintilla(sc,2154)
  {
   Scintilla(sc,2079)
   break
  }
  Scintilla(sc,2126,a_index,indent)
 }
 if indent>0
 tooltip,Segment is open,0,0,10
 else
 ToolTip,,,,10
 splashtextoff
 settimer,longwait,off
 return
 longwait:
 splashtexton,300,80,Please Wait,Fixing indentation`nthis could take a while for large Segments`nIf you see this message often consider turning off Auto Fix Indentation in options
 settimer,longwait,off
 return
}
fold(sc){
 static
 mousegetpos,x,y
 y-=52 , x+=4
 pos:=Scintilla(sc,2023,x,y)
 pos:=Scintilla(sc,2166,pos) 
 scintilla(sc,2231,(pos)) ;fold at carret
}
font(x=1){
 sc:=gui(1) , a:=object("name","Tahoma","style","s12")
 For i, v in a
 %i%:=iniread("font",i,v)
 color:=iniread("font",5)
 if x
 if !Dlg_Font(name,style,color,true,hwnd)
 return
 a:=["italic","bold","underline"]
 for b,c in a
 %c%:=if instr(style,c) ? 1:0
 if !regexmatch(style,"s(.*)",out)
 out1=10
 loop,40
 out:=a_index , scintilla(sc,2055,out,out1) , sendstring(sc,2056,out,name,"int") , Scintilla(sc,2054,a_index-1,italic) , Scintilla(sc,2053,a_index-1,bold) , Scintilla(sc,2059,a_index-1,underline)
 gui("name",name) , gui("style",style)
 dcolors("reload")
 if x
 setcolor(sc,5)
 else
 setcolor(sc)
}
Full_Backup(){
 save()
 backup=% currentproject(1) "\backup\Full Backup" a_now "\"
 filecreatedir,%backup%
 splashtexton,400,50,Cleaning up files,Please wait`n This can take a while for large projects.
 loop,% currentproject(1) "\*.*"
 if !InStr(A_LoopFileFullPath,".exe")
 filecopy,%a_loopfilefullpath%,%backup%
 loop,% currentproject(1) "\backup\*.*",2
 if a_loopfilename not contains full backup
 fileremovedir,%a_loopfilefullpath%,1
 splashtextoff
}
function_finder(){
 sc:=gui(1),start:=scintilla(sc,2266,scintilla(sc,2008)-1,1),end:=scintilla(sc,2267,scintilla(sc,2008)-1,1),d(2)
 while,out:=split(segkeeper(),"|",A_Index){
  script:=scripts(out)
  loop,parse,script,`n
  if (RegExMatch(A_LoopField,"(.*\(.*\).*{)",fun) && InStr(A_LoopField," ")=0)
  function.=substr(fun,1,-1) " "
 }
 sort,function,D%A_Space%
 sendstring(sc,2100,end-start,function,"int"),d(1)
}
get(x,y=1){
 d(x)
 lv_gettext(out,lv_getnext(),y)
 d(1)
 return out
}
Getline(sc){
 SendMessage, 2027,, 0,, ahk_id %sc%
 VarSetCapacity(text, ErrorLevel)
 SendMessage, 2027, ErrorLevel, &text,, ahk_id %sc%
 return StrGet(&text,cp0) 
}
getper(x="ftpfile.ftp"){
 filegetsize,size,%x%,K
 hash:=round(size/2.048)
 fileread,out,ftplog.txt
 regexmatch(out,"#(.*)#",hashes)
 progress,% (strlen(hashes))*100/hash,Uploading,Uploading file %x%,Uploading
}
getpos(x=0,y=0,z=0){
 static
 sc:=gui(1)
 if (x="load"){
  if !IsObject(pos)
  pos:=[]
  pro:=currentproject()
  position:=iniread("cursor position","position",0,1)
  while,p:=split(position,"|",a_index){
   if !IsObject(pos[pro])
   pos[pro]:=[]
   pos[pro][split(p,"=",1)]:=split(2)
  }
  return
 }
 if (x && !y){
  return pos[currentproject()][x]
 }
 if !IsObject(pos[currentproject()])
 pos[currentproject()]:=[]
 pos[currentproject()][currentsegment()]:=Scintilla(sc,2143) "," Scintilla(sc,2152)
 if z=1
 {
  for a in Pos
  {
   SplitPath,a,,dir
   list=
   for key,value in pos[a]
   if value <> 0,0
   list.=key "=" value "|"
   iniwrite,%list%,% dir "\options.ini",Cursor Position,Position
  }
 }
}
getrange(start,end){
 return substr(scripts(currentsegment()),start+1,end-start)
}
go(){
 d(99)
 LV_GetText(out,LV_GetNext())
 select(out,2)
 gui,99:destroy
 ControlFocus,Scintilla1,Script Writer.ahk
}
gui(x="",y=""){
 static
 gui,submit,nohide
 if x=1
 return sc
 if (x="options" && y)
 return options.=y
 if y
 {
  %x%=%y%
  return %x%
 }
 if x
 {
  out:=%x%
  return out
 }
 Version=0.200.102
 ;If you put     ;auto_version    anywhere in your script it will add the version when you upload it.
 menu=f,&New|f,&Open|f,&Close|f,New S&egment|f,Create Se&gment From Selection|f,&Open|f,&Save|f,Open Fol&der|f,&Run|f,&Version|f,&Full Backup|f,&Publish|f,&Auto Update|f,&Upload|f,Post to A&hk.net|f,Expor&t|f,Export P&lugin|e,Fo&nt|e,Fi&x Indent|e,R&eplace Text|e,&Find|e,Fin&d in Segment|e,Ed&it Replacements|e,Cu&t|e,Edit &Highlight Text|e,Edit Co&lors|e,&Copy|e,&Jump|e,Paste	Ctrl &V|e,&Restore|e,Remove Current &Segment|e,&Plugins|e,Insert F&unction|e,Ch&aracter Count|a,&Help|a,&About|w,&Variables|w,&Ideas|f,E&xit|o,&Settings|o,&Auto Fix Indentation|o,&Highlight Matching Brace|o,&Word Wrap|o,&Context Sensitive Help|o,&End of line|o,&Debug
 options=Next variable,Main|Previous variable,Main|Add variable,Main|Insert variable,Main|Function Finder,Main|Find Again,Main|Run,Main|Insert Function,Main|Find,Main|Testing,Main|Reload,Main|Character Count,Main
 IfWinNotExist %A_ScriptName%
 gui,add,statusbar,,I am looking for ideas for this space
 sb_setparts(200,200,200)
 Gui, +LastFound resize
 windows=main,Segments,Variables,Ideas,Search
 indent:=iniread("indent","level",1)
 position:=IniRead("position")
 loop,parse,position,`n
 {
  y:=split(A_LoopField,"=",1),z:=split(2)
  loop,parse,z,%a_space%
  {
   posit:=substr(a_loopfield,1,1),value:=substr(a_loopfield,2)
   if value is number
   %y%.=posit value " "
  }
 }
 main:=if main ? main : "x100 y100 w" a_screenwidth-400 " h" a_screenheight-250 " "
 segments:=if segments ? segments : "x" a_screenwidth-285 " y100 w180 h" a_screenheight-230 " "
 ideas:=if ideas ? ideas : "x" a_screenwidth-485 " y100 w180 h" 200 " "
 search:=if search ? search : "w300 h500"
 Gui,Add,Tab,x0 y0 h23 Buttons -Wrap gproject vtab
 Gui,Add,Tab,x0 y23 h23 Buttons -Wrap gtab vseg
 sc:=Scintilla_Add(WinExist(), 0, 46, 500, 500)
 lastopen:=iniread("lastopen","file",0)
 splitpath,lastopen,filename,dir,,noext
 ;Window("2,Segments," segments " hide,listview~-multi -hdr altsubmit x0 y0 ggh vselseg ~Segments")
 Window("3,Variables," variables " hide,listview~-multi altsubmit ggh vcv x0 y0~Variables")
 Window("4,Ideas," ideas " hide,Edit~videa ggh x0 y0 multi~")
 autoopen(lastopen)
 menu(menu)
 Gui,Show,%main% hide
 settitlematchmode,1
 out=2246,2,1|2242,0,0|2242,1,15|2155,0|2069,0xFFFFFF|2242,0,0|2155,,0|2132,1|2460,3|2462,1|2134,1|2260,1|2264,1000|2122,indent|2037,65001|2040,1,7|2115,1
 string(sc,out)
 OnMessage(0x4E, "OnWmNotify")
 scintilla(sc,2242,0,sendstring(sc,2276,33,"_" scintilla(sc,2154),"int"))
 options()
 if iniread("visible","segments") = "error"
 iniwrite("visible","segments",1)
 while,x:=split(windows,",",a_index),y:=iniread("visible",x,0)
 if y
 winshow %x%
 winactivate,%a_scriptname%
 winwaitactive,%a_scriptname%
 markers()
 gui,1:show,%main%
 winget,id,id,%a_scriptname%
 iniread,out,% currentproject(1) "\options.ini",lastopen,segment,1
 if out=1
 {
  d(2)
  lv_modify(1,"select")
 }
 else
 select(out,2)
 gui_load=1
 ControlFocus,Scintilla1,Script Writer.ahk
 return
 GuiClose:
 save()
 saveposition()
 ExitApp
 return
 tab:
 Gui,Submit,NoHide
 currentsegment(seg)
 currentproject(tab)
 splitpath,tab,,dir
 iniwrite,%seg%,%dir%\options.ini,lastopen,segment
 scintilla(sc,2358,"",winnumber(seg))
 select(seg,2)
 return
 project:
 SetTimer,project,Off
 GuiControl,Hide,SysTabControl322
 Gui,Submit,NoHide
 currentsegment(seg),currentproject(tab),segments:=segkeeper()
 GuiControl,1:,SysTabControl322,|%segments%
 d(3)
 dir:=currentproject(1)
 IniRead,variables,%dir%\options.ini,variables
 LV_Delete(),variable:=lastvariable(currentproject())
 while,x:=split(variables,"`n",a_index),add:=split(x,"=",2){
  select:=add=variable?"Select":""
  lv_add(select,add)
 }
 d(1)
 select(lastseg(tab),2)
 ControlSetText,Edit1,% ideaholder(tab),Ideas
 GuiControl,1:Show,SysTabControl322
 return
}
guiclose(){
 wingettitle,out,A
 togglevisible(out)
 winhide,%out%
}
help(){
 Run,"http://code.google.com/p/script-writer/"
}
hk(){
 d(7)
 hotkey:=window(7,"hk")
 if !lv_getnext()
 return
 lv_gettext(out,lv_getnext(),1)
 lv_modify(lv_getnext(),"",out,hotkey)
 iniwrite,%hotkey%,options.ini,hotkeys,%out%
 lv_modifycol(2,"autohdr")
}
hotkey(x,y){
 id:=gui("id")
 d(7)
 while,z:=split(y,"|",a_index)
 {
  stringsplit,key,z,`,
  hotkey:=iniread("hotkeys",key1," ")
  key:=stringreplace(key1," ","_",1)
  keymap(hotkey,key)
  out:=if key2="main" ? a_scriptname : key2
  hotkey,ifwinactive,%out%
  if x=2
  lv_add("",key1,hotkey,key2)
  if !hotkey
  continue
  if x=1
  hotkey,%hotkey%,keymap,on
  if x=0
  Hotkey,%hotkey%,keymap,off
 }
 Hotkey,ifwinactive,%a_scriptname%
 Hotkey,rbutton,rightclickmenu,on
 Hotkey,^f,find,On
 Hotkey,^v,paste,On
 Hotkey,^x,cut,On
}
Ideas(){
 togglevisible(a_thisfunc)
}
idea(){
 ControlGetText,out,Edit1,Ideas
 ideaholder(currentproject(),out)
}
ideaholder(x,y=""){
 static ideas:=[]
 if !y
 return ideas[x]
 ideas[x]:=y
}
indent(){
 controlgettext,out,Edit4,A
 iniwrite("indent","level",out)
 gui("indent",out)
}
iniread(x,y="",z="",dir=""){
 file:=currentproject()
 splitpath,file,,directory
 out:=if dir ? directory "\options.ini" : "options.ini"
 iniread,out,%out%,%x%,%y%,%z%
 return out
}
iniwrite(x,y,z,dir=0){
 out:=if dir ? currentproject(1) "\options.ini" : "options.ini"
 iniwrite,%z%,%out%,%x%,%y%
}
insert_function(){
 sc:=gui(1)
 inputbox,out,New Function Name?,Input your new function name (spaces will be replaced by _)
 out:=clean1(out)
 if !out
 exit
 inputbox,out1,Parameters?
 sendstring(sc,2003,scintilla(sc,2008),out "(" out1 ")" chr(123) "`r`n`r`n" chr(125),"int")
 send,{down}{space}
}
insert_variable(){
 SetKeyDelay,-1
 send,% get(3)
 SetKeyDelay,1
}
insertvariable(){
 sc:=gui(1)
 if scintilla(sc,2102)
 {
  send,{enter}
  if scintilla(sc,2007,scintilla(sc,2266,scintilla(sc,2008),1)-1)!=37
  sendstring(sc,2003,scintilla(sc,2266,scintilla(sc,2008),1),"%","int")
  send,`%
  return update()
 }
 d(3),sc:=gui(1),hotkeys:=keywords(99)
 loop,Parse,hotkeys," "
 if A_LoopField contains a_
 list.=a_loopfield " "
 while,LV_GetText(var,A_Index)
 list.=var " "
 Sort,list,D%A_Space%
 sendstring(sc,2100,0,list,"int"),d(1)
}
install(x=""){
 static
 if x
 return installed
 filecreatedir,%A_ScriptDir%\plugins
 file:=get(11)
 filemove,downloads\%file%,plugins\%file%,1
 out:=read("plugins.ini"),inc:=chr(35) "include"
 if out not contains \%file%
 fileappend,%inc% *i plugins\%file%`n,plugins.ini
 msgbox Plugin installed
 installed=1
}
jump(x=0){
 list:=segkeeper()
 sort,list,D|
 window("99,Jump,,Edit~vsel w200 ggh r1~,ListView~w200 -multi r9 vname~Segment,Button~Default vgo ggh~Jump")
 d(99)
 Loop,Parse,list,|
 lv_add("",A_LoopField)
 control,choose,1,ListBox1,Jump,Jump
 Hotkey,IfWinActive,Jump
 Hotkey,Down,jumpsel,On
 Hotkey,Up,jumpsel,On
 LV_Modify(1,"Select")
 return
 99guiescape:
 gui,99:destroy
 return
 jumpsel:
 d(99)
 next:=if A_ThisHotkey="Down" ? LV_GetNext()+1 : LV_GetNext()-1
 next:=if next > 0 ? next : 1
 LV_Modify(next,"Select Vis")
 return
}
jump_to_next_marker(){
 next:=scintilla(gui(1),2047,cline()+1,32)
 scintilla(gui(1),2024,next)
}
jump_to_previous_marker(){
 next:=scintilla(gui(1),2048,cline()-1,32)
 scintilla(gui(1),2024,next) 
}
keymap(x=0,y=0){
 static
 if !hotkey
 hotkey:=object()
 if (x && y)
 hotkey[x]:=y
 else
 hotkey[x]()
}
keywords(x){
 static
 if (!isobject(keywords) || x=98)
 {
  keywords:=object()
  1=autotrim blockinput break clipwait continue controlclick controlfocus controlget controlgetfocus controlgetpos controlgettext controlmove controlsend controlsendraw controlsettext coordmode critical detecthiddentext detecthiddenwindows drive driveget drivespacefree edit else endrepeat envadd envdiv envget envmult envset envsub envupdate exit exitapp fileappend filecopy filecopydir filecreatedir filecreateshortcut filedelete filegetattrib filegetshortcut filegetsize filegettime filegetversion fileinstall filemove filemovedir fileread filereadline filerecycle filerecycleempty fileremovedir fileselectfile fileselectfolder filesetattrib filesettime formattime getkeystate gosub goto groupactivate groupadd groupclose groupdeactivate gui guicontrol guicontrolget hideautoitwin hotkey if ifequal ifexist ifgreater ifgreaterorequal instr ifinstring ifless iflessorequal ifmsgbox ifnotequal ifnotexist ifnotinstring ifwinactive ifwinexist ifwinnotactive ifwinnotexist imagesearch inidelete iniread iniwrite input inputbox keyhistory keywait listhotkeys listlines listvars loop menu mouseclick mouseclickdrag mousegetpos mousemove msgbox onexit outputdebug pause pixelgetcolor pixelsearch postmessage process progress random regdelete regexmatch regexreplace regread regwrite reload repeat return run runas runwait send sendmessage sendraw sendinput setbatchlines setcapslockstate setcontroldelay setdefaultmousespeed setenv setformat setkeydelay setmousedelay setnumlockstate setscrolllockstate setstorecapslockmode settimer settitlematchmode setwindelay setworkingdir shutdown sleep sort soundbeep soundget soundgetwavevolume soundplay soundset soundsetwavevolume splashimage splashtextoff splashtexton splitpath statusbargettext statusbarwait stringcasesense stringgetpos stringleft stringlen stringlower stringmid stringreplace stringright stringsplit stringtrimleft stringtrimright stringupper suspend sysget thread tooltip transform traytip tv_add tv_modify tv_delete tv_getselection tv_getcount tv_getparent tv_getchild tv_getprev tv_getnext tv_gettext tv_get urldownloadtofile winactivate winactivatebottom winclose winget wingetactivestats wingetactivetitle wingetclass wingetpos wingettext wingettitle winhide winkill winmaximize winmenuselectitem winminimize winminimizeall winminimizeallundo winmove winrestore winset winsettitle winshow winwait winwaitactive winwaitclose winwaitnotactive purple
  2=a_ahkversion a_autotrim a_batchlines a_caretx a_carety a_computername a_controldelay a_cursor a_dd a_ddd a_dddd a_defaultmousespeed a_desktop a_desktopcommon a_detecthiddentext a_detecthiddenwindows a_endchar a_eventinfo a_exitreason a_formatfloat a_formatinteger a_gui a_guicontrol a_guicontrolevent a_guievent a_guiheight a_guiwidth a_guix a_guiy a_hour a_iconfile a_iconhidden a_iconnumber a_icontip a_index a_ipaddress1 a_ipaddress2 a_ipaddress3 a_ipaddress4 a_isadmin a_iscompiled a_issuspended a_keydelay a_language a_linefile a_linenumber a_loopfield a_loopfileattrib a_loopfiledir a_loopfileext a_loopfilefullpath a_loopfilelongpath a_loopfilename a_loopfileshortname a_loopfileshortpath a_loopfilesize a_loopfilesizekb a_loopfilesizemb a_loopfiletimeaccessed a_loopfiletimecreated a_loopfiletimemodified a_loopreadline a_loopregkey a_loopregname a_loopregsubkey a_loopregtimemodified a_loopregtype a_mday a_min a_mm a_mmm a_mmmm a_mon a_mousedelay a_msec a_mydocuments a_now a_nowutc a_numbatchlines a_ostype a_osversion a_priorhotkey a_programfiles a_programs a_programscommon a_screenheight a_screenwidth a_scriptdir a_scriptfullpath a_scriptname a_sec a_space a_startmenu a_startmenucommon a_startup a_startupcommon a_stringcasesense a_tab a_thishotkey a_thismenu a_thismenuitem a_thismenuitempos a_tickcount a_timeidle a_timeidlephysical a_timesincepriorhotkey a_timesincethishotkey a_titlematchmode a_titlematchmodespeed a_username a_wday a_windelay a_windir a_workingdir a_yday a_year a_yweek a_yyyy abort abs acos add ahk_class ahk_group ahk_id ahk_pid alnum alpha alt altdown altsubmit alttab alttabandmenu alttabmenu alttabmenudismiss altup alwaysontop appskey asc asin atan background backspace between bitand bitnot bitor bitshiftleft bitshiftright bitxor blind border bottom browser_back browser_favorites browser_forward browser_home browser_refresh browser_search browser_stop bs button buttons byref cancel capacity capslock caption ceil center check check3 checkbox checked checkedgray choose choosestring chr click clipboard clipboardall close color combobox contains control controllist cos count ctrl ctrlbreak ctrldown ctrlup date datetime days ddl default del delete deleteall delimiter deref destroy digit disable disabled down dropdownlist eject enable enabled end enter error errorlevel esc escape exp exstyle f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12
  3=allowsamelinecomments clipboardtimeout commentflag errorstdout escapechar hotkeyinterval hotkeymodifiertimeout hotstring include installkeybdhook installmousehook maxhotkeysperinterval maxmem maxthreads maxthreadsbuffer maxthreadsperhotkey noenv notrayicon persistent singleinstance usehook winactivateforce
  Loop,3
  keywords[a_index]:=%a_index%
  while,out:=split(iniread("keywords"),"`n",a_index)
  keywords[a_index+3]:=split(out,"=",2)
  commands:=read("commands.ini")
  Loop,parse,commands,`n,`r
  out:=split(a_loopfield," ",1) , list.=if instr(list," " out " ") ? "" : out " "
  Sort,list,U
  keywords[99]:=list
 }
 out:=keywords[x]
 sort,out,D%a_space%
 return out
}
keyword_set(){
 if a_guievent in normal,i
 {
  list:=object(1,9,2,10,3,14)
  cst("Edit1",iniread("keywords",get(12)," "),"Highlight text editor")
  d(12)
  windowcolor(12,rgb(dcolors(list[lv_getnext()])),"Highlight text editor")
 }
}
line_numbers_background(){
 iniwrite("font","linenumbers",Dlg_Color(dcolors("linenumbers")))
 dcolors(99)
 setcolor(gui(1))
 setcolor(Edit_Colors("font_color"))
}
loop(x,y,z=1){
 d(x)
 lv_gettext(out,y,z)
 return out
}
markers(){
 string(gui(1),"2244,2,0xFE000000|2040,25,13|2040,26,15|2040,27,11|2040,28,10|2040,29,9|2040,30,12|2040,31,14|2233,16")
}
menu(m){
 loop,parse,m,|
 {
  if A_Index=3
  recent_files()
  split(A_LoopField,",",1)
  if (A_IsCompiled && split(2)="&Plugins")
  continue
  menu,% split(1),add,% split(2),menu
 }
 menu,bar,add,&File,:f
 menu,bar,add,&Edit,:e
 menu,bar,add,&About,:a
 menu,bar,add,&Options,:o
 menu,bar,add,&Window,:w
 gui,1:menu,bar
}
mm(){
 out:=regexreplace(regexreplace(A_ThisMenuItem,"i)(?:[^a-z0-9 ]|\&)","")," ","_")
 if (a_thismenu = "o" && a_thismenuitem != "&settings")
 options(a_thismenuitem)
 else
 %out%()
}
clean1(x){
 return regexreplace(regexreplace(x,"i)(?:[^a-z0-9 ]|\&)","")," ","_")
}
new(){
 save()
 inputbox,out,Create a new project,Please name your new project
 newfile=projects\%out%
 ifexist,%newfile%
 {
  msgbox Project already exists.  Please input another
  return
 }
 filecreatedir,%newfile%
 fileappend,,%newfile%\%out%
 inc:=chr(35) "include"
 fileappend,%inc% %out%`n,%newfile%\%out%.ahk
 iniwrite("lastopen","file",A_ScriptDir "\" newfile "\" out ".ahk")
 iniwrite,1,%newfile%\options.ini,lastopen,segment
 autoopen(A_ScriptDir "\" newfile "\" out ".ahk")
}
new_segment(x=""){
 sc:=gui(1)
 if !x
 inputbox,out,New Segment,name?
 if ErrorLevel
 return
 out:= x ? x : clean1(out)
 if !out
 return
 ifexist,% currentproject(1) "\" out
 {
  msgbox File exists.   Please choose another
  exit
 }
 window:=Scintilla(sc,2375),segkeeper(currentproject(),segkeeper() out "|")
 GuiControl,,SysTabControl322,% "|" segkeeper()
 winnumber(out,window)
 inc:=chr(35) "include"
 fileappend,%inc% %out%`n,% currentproject()
 select(out,2)
 sleep,100
 set(sc)
}
OnWmNotify(wParam, lParam, msg, hwnd){
 critical
 sc:=gui(1)
 font_color:=edit_colors(1)
 if NumGet(lParam+0) != sc
 if numget(lparam+0) != font_color
 return
 code:=NumGet(lParam+8)
 if code=2022
 {
  scintilla(sc,2104)
  if scintilla(sc,2007,start:=scintilla(sc,2266,end:=scintilla(sc,2008),1)-1)=37
  send,% chr(37)
  update()
 }
 if code=2001
 {
  char:=scintilla(sc,2007,scintilla(sc,2008)-1)
  if (char=40 && scintilla(sc,2102)){
   scintilla(sc,2104)
   send,% Chr(40)
   update()
  }
  update()
  if char=37
  insertvariable()
  if char=10
  enter()
  if char in 123,125
  brace()
  if char in % sv("end") "32,44,40,37"
  replaceandcontext(char)
  sendstring(sc,2077,"","qwertyuiopasdfghjklzxcvbnm_#QWERTYUIOPASDFGHJKLZXCVBNM"),start:=scintilla(sc,2266,scintilla(sc,2008)-1,1),end:=scintilla(sc,2267,scintilla(sc,2008)-1,1),text:=scripts(currentsegment()),out:=substr(text,start+1,end-start)
  if (scintilla(sc,2102) || scintilla(sc,2202))
  return
  kwords:=keywords(99)
  stringreplace,wordz,kwords,%a_space%,`,,all
  scintilla(sc,2115,1)
  if end-start > 1
  {
   words:=keywords(99),d(3)
   while,LV_GetText(out,A_Index)
   words.=out " "
   loop,parse,words,%a_space%
   {
    if !A_LoopField
    continue
    if regexmatch(a_loopfield,"Ai)" getrange(start,end))
    send.=A_LoopField " "
   }
   StringTrimRight,send,send,1
   if send
   sendstring(sc,2100,end-start,send,"int")
  }
 }
 if code=2010
 {
  wingetactivetitle,out
  if out contains %a_scriptname%
  {
   fold(sc)
   return
  }
  iniwrite("font",33,Dlg_Color(iniread("font",33,dcolors(33))))
  dcolors("reload")
  setcolor(sc,33)
  setcolor(font_color,33)
 }
 if code=2008
 {
  update()
 }
 if code = 2027
 {
  out:=scintilla(font_color,2010,scintilla(font_color,2008))
  iniwrite("font",out,Dlg_Color(iniread("font",out,dcolors(out))))
  dcolors("reload")
  setcolor(sc,out)
  setcolor(font_color,out)
 }
 if code=2007
 {
  ControlGet,line,currentline,,Scintilla1,%A_ScriptName%
  SB_SetText("Row:" line " Column:" scintilla(sc,2129,scintilla(sc,2008)),1)
  if options("&highlight matching brace",1)
  while,sub:=split("2,1",",",a_index)
  {
   sub-=1
   if scintilla(sc,2353,scintilla(sc,2008)-sub,0) < scintilla(sc,2006)
   {
    scintilla(sc,2351,scintilla(sc,2008)-sub,scintilla(sc,2353,scintilla(sc,2008)-sub))
    return
   }
   scintilla(sc,2352,-1)
  }
  getpos()
 }
 if code=2004
 {
  msgbox,Please either create a new document or open an existing one
  return
 }
 if code = 2016
 dwellmessage(sc)
 if code = 2017
 dwellmessage(sc,1)
}
open(){
 fileselectfile,out,,projects,Select project to open,*.ahk
 if !out
 return
 if InStr(projects(),out){
  GuiControl,ChooseString,SysTabControl321,%out%
  SetTimer,project,1
  return
 }
 loop,read,%out%
 if a_loopreadline not contains `#include
 {
  splitpath,out,filename,,,name
  ifexist,%a_scriptdir%\projects\%name%\%filename%
  {
   openerror:
   msgbox project already exists
   InputBox,name,Rename project,Please choose another name for the project,,,,,,,,%name%
   if ErrorLevel
   Exit
   filename=%name%.ahk
   IfExist,projects\%name%
   goto openerror
  }
  import(out,name)
  iniwrite("lastopen","file",a_scriptdir "\projects\" name "\" filename)
  return autoopen(a_scriptdir "\projects\" name "\" filename,1)
  reload
 }
 ;save(1)
 add_recent(out)
 iniwrite("lastopen","file",out)
 return autoopen(out,1)
 reload
}
open_folder(){
 run,% currentproject(1)
}
options(x=0,y=0){
 static
 function:=clean1(x)
 if !options
 {
  options:=object()
  while,list:=split(gui("menu"),"|",a_index)
  {
   if substr(list,1,1)="o"
   while,x:=split(iniread("options"),"`n",a_index)
   if (split(x,"=",1) = clean1(substr(list,3)) && split(2)=1)
   {
    menu,o,togglecheck,% substr(list,3)
    options[substr(list,3)]:=split(2)
    function:=clean1(substr(list,3))
    if function!=debug
    %function%()
   }
  }
 }
 if y
 return options[x]
 if x
 {
  options[x]:=if options[x] ? 0 : 1
  iniwrite("options",function,options[x])
  menu,O,togglecheck,%x%
  if isfunc(function)
  %function%()
 }
}
optionswitch(x){
 if x=settings
 settings()
 else
 %x%()
}
paste_ctrl_v(){
 send,^v
 return
 paste:
 paste_ctrl_v()
 update()
 if options("&auto fix indentation",1)
 fix_indent()
 if scintilla(sc:=gui(1),2007,scintilla(sc,2008)-1)=10
 Send,^{Right}
 return
}
plugins(){
 window("11,Plugins,,listview~vshowprogram ggh altsubmit w150 h500~Plugins,edit~w500 h500 x+5~,button~x10 ggh vdownload_list~Download New Plugins")
 d(11)
 loop,plugins\*.ahk
 lv_add("",a_loopfilename)
 return
 11guiescape:
 11guiclose:
 save()
 if install(1)
 {
  msgbox,4,Plugin,To activate new plugins you must restart the program`nRestart?,0
  ifmsgbox yes
  reload
 }
 else
 gui,11:destroy
 return
}
previous_variable(){
 d(3)
 if lv_getnext() > 1
 lv_modify((lv_getnext()-1),"select"),LV_GetText(variable,LV_GetNext())
 IniWrite,%variable%,% currentproject(1) "\options.ini",lastopen,variable
}
next_variable(){ 
 d(3)
 lv_modify((lv_getnext()+1),"select"),LV_GetText(variable,LV_GetNext())
 IniWrite,%variable%,% currentproject(1) "\options.ini",lastopen,variable
}

publish(x=""){
 d(2)
 dir:=currentproject(1)
 ifexist,%x%
 filedelete,%x%
 segments:=segkeeper()
 while,out:=split(segments,"|",A_Index)
 publish.=scripts(out) "`r`n"
 iniread,v,%dir%\options.ini,version,number,0.0
 publish:=stringreplace(publish,";auto_version","Version=" v)
 if x=quiet
 return publish
 if x
 {
  if x=1
  x=ftpfile.ftp
  filedelete,%x%
  fileappend,%publish%,%x%
  return
 }
 else
 {
  clipboard:=publish
  splashtexton,240,40,%a_scriptname%,Script coppied to clipboard
  sleep,2000
  splashtextoff
 }
}
rcmhandle(){
 out:=clean(a_thismenuitem)
 %out%()
}
read(x){
 fileread,out,%x%
 return out
}
recent_files(){
 static
 read:=if IniRead("recent") ? IniRead("recent") : "x=none yet"
 while,x:=split(read,"`n",A_Index)
 menu,recentl&y open,add,%x%,recent
 menu,f,add,Recentl&y Opened,:recentl&y open
 return
 recent:
 if A_ThisMenuItem = none yet
 return
 ;save()
 add_recent(split(A_ThisMenuItem,"=",2))
 ;IniWrite("lastopen","file",split(a_thismenuitem,"=",2))
 return autoopen(split(a_thismenuitem,"=",2),1)
 return
}
redo(){
 send,^y
}
reg(x,y){
 regexmatch(x,y,out)
 return out1 out2
}
reload(){
 reload
}
remove_current_segment(){
 msgbox,4,Delete segment!!!,Are you sure you wish to delete this segment?
 ifmsgbox no
 exit
 ControlGet,tab,tab,,SysTabControl322,%A_ScriptName%
 replace:=currentsegment() "|",segments:=segkeeper()
 StringReplace,segments,segments,%replace%
 tab:=tab>1 ? tab-1 : 1
 GuiControl,1:,SysTabControl322,|%segments%
 GuiControl,Choose,SysTabControl322,%tab%
 msgbox % gui("seg")
 select(Gui("seg"),2)
 ControlGetText,out,SysTabControl322,%a_scriptname%
 filedelete,% currentproject()
 filedelete,% currentproject(1) "\" currentsegment()
 while,file:=split(segments,"|",A_Index)
 list.=chr(35) "include " file "`n"
 fileappend,%list%,% currentproject()
 segkeeper(currentproject(),segments)
}
remove_marker(){
 scintilla(gui(1),2044,cline(),5)
}
replace(x=""){
 static
 if (!isobject(repl) || x=1)
 {
  repl:=object() , rep:=iniread("replacements")
  loop,parse,rep,`n
  repl[split(a_loopfield,"=",1)]:=split(2)
 }
 if x
 return repl[x]
}
replaceandcontext(x){
 static commands
 if !isobject(commands)
 {
  commands:=object() , com:=read("commands.ini")
  loop,parse,com,`n
  commands[split(a_loopfield," ",1)]:=a_loopfield
 }
 if x is not number
 return commands[x]
 sc:=gui(1),sendstring(sc,2077,"","qwertyuiopasdfghjklzxcvbnm_#QWERTYUIOPASDFGHJKLZXCVBNM"),start:=scintilla(sc,2266,scintilla(sc,2008)-1,1),end:=scintilla(sc,2267,scintilla(sc,2008)-1,1),text:=scripts(currentsegment()),out:=substr(text,start+1,end-start)
 if replace(out)
 {
  scintilla(sc,2160,start,end+1)
  sendstring(sc,2170,"",replace(out) chr(x),"int")
  update()
  text:=scripts(currentsegment())
 }
 if (scintilla(sc,2007,start:=scintilla(sc,2266,scintilla(sc,2008)-1)-1)=37 && scintilla(sc,2007,end:=scintilla(sc,2008)-1)!=37 && trim(getrange(start,end))!="%")
 sendstring(sc,2003,scintilla(sc,2008)-1,Chr(37),"Int")
 if !options("&context sensitive help",1)
 return
 ControlGet,line,currentline,,Scintilla1,%A_ScriptName%
 home:=scintilla(sc,2128,line-1),current:=scintilla(sc,2008)
 while current > home , current--
 {
  if scintilla(sc,2007,current) = 41
  current:=scintilla(sc,2353,current),current:=scintilla(sc,2266,current)
  if scintilla(sc,2007,current) = 40
  {
   start:=scintilla(sc,2266,current,1),end:=scintilla(sc,2267,current,1)
   command:=substr(scripts(currentsegment()),start+1,end-start)
   if !commands[command]
   continue
   if commands[command]
   sendstring(sc,2200,home,commands[command],"int")
   commas:=RegExReplace(SubStr(scripts(currentsegment()),current+2,scintilla(sc,2008)-current-1),"U)\((.*)\)")
   highlight(commas,commands[command],1)
   return
  }
 }
 start:=home+1,end:=scintilla(sc,2267,start,1)
 command:=substr(scripts(currentsegment()),start,end-start+1)
 if commands[command]
 sendstring(sc,2200,home,commands[command],"int")
 highlight(SubStr(scripts(currentsegment()),current+2,scintilla(sc,2008)-current-1),commands[command])
 return
}
highlight(x,cmd,function=""){
 brace:=0
 search=U)"(.*)"
 search:=RegExReplace(x,search)
 loop,parse,search
 {
  if A_LoopField=(
  brace++
  if brace=0
  commas.=A_LoopField
  if A_LoopField=)
  brace--
 }
 sc:=gui(1)
 RegExReplace(commas,",","",commas)
 if (function=1 && commas=0)
 start:=instr(cmd,"(",1)
 else
 start:=instr(cmd,",",0,1,commas)
 if !start
 return scintilla(sc,2204,1,1)
 out:=if instr(cmd,",",0,1,commas+1)=0 ? StrLen(cmd)-1 : instr(cmd,",",0,1,commas+1)
 scintilla(sc,2204,start,out)
}
replace_text(){
 static
 window("77,Replace Text,,text~x5~Find text:,edit~x+5 w150~,text~x5~Replace with:,edit~x+5 w150~,button~ggh vfindandreplace default~Find and Replace")
}
res(){
 sc:=gui(1)
 cst("Edit1",read(get(7) "\" currentsegment()),"Restore the current file")
 update()
}
restore(){
 Window("7,Restore the current file,w950 h525,listview~w550 h480 altsubmit vres ggh~Backup,edit~x+10 w480 h480~,button~x10 ggh vrestorefile~Restore selected file")
 loop,% currentproject(1) "\backup\" currentsegment(),1,1
 {
  d(7)  
  lv_add("",a_loopfiledir)
 }
}
restorefile(){
 controlgettext,out,Edit1,A
 sendstring(gui(1),2181,,out)
}
rgb(c){
 setformat,IntegerFast,H
 c:=(c&255)<<16 | (c&65280) | (c>>16),c:=SubStr(c,1)
 SetFormat, integerfast,D
 return c
}
rightclickmenu(){
 list=Copy,Paste 	Ctrl ^V,Undo,Redo,________,Add marker,Remove marker,Jump to next marker,Jump to previous marker
 while,x:=split(list,",",a_index)
 menu,rcm,add,%x%,rcmhandle
 menu,rcm,show
}
run(){
 save(1)
 file:=currentproject()
 if InStr(file,"script writer."){
  Reload
  ExitApp
 }
 SplitPath,file,file,dir 
 if options("&debug",1){
  for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
  if InStr(process.CommandLine,currentproject()){
   IfWinExist,% "ahk_pid " process.ProcessId
   WinKill,% "ahk_pid " process.ProcessId
   Process,Close,% process.processid
  }
  settimer,debug,on
  return
 }
 file:=currentproject()
 run,"%file%",% dir
}
save(x=""){
 Pause,Off
 if x
 saveposition()
 backup:=currentproject(1) "\backup\" a_now A_MSec
 filecreatedir,%backup%
 while,x:=split(update(1),",",a_index){
  if !x
  continue
  stringsplit,file,x,~
  if !(file1 && file2)
  continue 
  while,filename:=split(projects(),"|",a_index){
   splitpath,filename,,dir,,name
   if file2=% clean1(name)
   break
  }
  filemove,%dir%\%file1%,%backup%\%file1%,1
  fileappend,% scripts(clean1(name),file1,1),%dir%\%file1%
 }
 while,filename:=Split(projects(),"|",A_Index){
  SplitPath,filename,,dir
  filedelete,% dir "\ideas.ini"
  fileappend,% ideaholder(filename),% dir "\ideas.ini"
 }
 IniWrite("lastopen","file",projects())
 IniWrite("lastopen","currentproject",currentproject())
 update(2)
 d(2)
 getpos(0,0,1)
}
saveoption(x){
 controlgetfocus,out,Settings
 controlgettext,out,%out%,Settings
 iniwrite,%out%,options.ini,ftp,%x%
}
saveposition(){
 IniDelete,options.ini,position
 while,x:=split(gui("windows"),",",a_index)
 {
  win:=if x="main" ? a_scriptname : x
  winget,id,processname,%win%
  if id != autohotkey.exe
  continue
  wingetpos,xx,yy,,,%win%
  out:="x" xx " y" yy " " size(x,1)
  if size(x,1)
  iniwrite,%out%,options.ini,position,%x%
 }
}
saverep(){
 d(8)
 out2=
 inidelete,options.ini,replacements
 loop,% count(8)
 iniwrite("replacements",loop(8,a_index),loop(8,a_index,2))
 replace(1)
}
Scintilla(v="",w="",x="",y="",z=""){
 SendMessage,%w%,%x%,%y%,%z%,ahk_id %v%
 return errorlevel
}
Scintilla_Add(ParentWindowID, X, Y, W, H){
 Static ScintillaIndex := 0
 Scintilla_Init()
 return DllCall("CreateWindowEx" ,"int",0x200 ,"str","Scintilla"
 ,"str","Scintilla" . ++ScintillaIndex ,"int", 0x52310000
 ,"int",X ,"int",Y ,"int",W ,"int",H ,"uint",ParentWindowID
 ,"uint",0 ,"uint",0 ,"uint",0)
}
Scintilla_GetSelText(scWnd){
 SendMessage, 2161,, 0,, ahk_id %scWnd%
 VarSetCapacity(text, ErrorLevel)
 SendMessage, 2161,, &text,, ahk_id %scWnd%
 return text
}
Scintilla_GetText(scWnd){
 SendMessage, 2182,, 0,, ahk_id %scWnd%
 VarSetCapacity(text, ErrorLevel)
 SendMessage, 2182, ErrorLevel, &text,, ahk_id %scWnd%
 return strget(&text,out,utf-16)
}
Scintilla_Init(){
 if SCI_START !=
 return
 if !DllCall("GetModuleHandle","str","SciLexer.dll")
 DllCall("LoadLibrary","str","SciLexer.dll")
}
scripts(x,y=0,get=0){
 static
 static files:=[]
 z:=currentproject()
 SplitPath,z,,,,filename
 filename:=clean1(filename)
 if Get
 {
  return %x%[y]
 }
 if (z && y){
  SplitPath,z,,,,filename
  filename:=clean1(filename)
  if !IsObject(%filename%)
  %filename%:=object()
  %filename%[x]:=y
  if !files[filename]
  files[filename]:=1
 }
 if y=0
 return %filename%[x]
}
search(find,sort){
 d(2)
 GuiControl,5:-Redraw,TV
 segments:=segkeeper()
 loop,parse,segments,|
 {
  found:=A_LoopField
  out:=scripts(A_LoopField)
  if out contains %find%
  {
   d(5)
   if sort
   cat:=TV_Add(found)
   loop {
    stringgetpos,pos,out,%find%,L%a_index%
    if pos=-1
    break
    text:=substr(out,1,instr(out,"`r",0,(pos+1))-1)
    text:=substr(text,instr(text,"`r",0,0)+1)
    if !sort
    f%a_index%:=TV_Add(text)
    else
    f%a_index%:=TV_Add(text,cat)
    TV_Add((pos), f%a_index%)
    TV_Add(found, f%a_index%)
   }
  }
 }
 d(5)
 GuiControl, +Redraw, TV
 return
 5guiclose:
 5guiescape:
 gui,5:hide
 return
 5guisize:
 controlmove,Edit1,,,(a_guiwidth-10)
 controlmove,SysTreeView321,,,(a_guiwidth-10),(a_guiheight-80)
 controlmove,Sort,,%a_guiheight%
 controlmove,Button2,,,,%a_guiheight%
 size("search")
 return
}
seg(){
 d(2)
 lv_gettext(out,lv_getnext())
 d(1)
 return out
}
Segments(){
 togglevisible(a_thisfunc)
}
sel(){
 jump:=window(99,"sel")
 l:=segkeeper()
 sort,l,D|
 loop,parse,l,|
 list.=if (instr(A_LoopField,jump) || !jump) ? "|" A_LoopField :
 d(99)
 sort,list,D|
 LV_Delete()
 Loop,Parse,List,|
 if A_LoopField
 LV_Add("",A_LoopField)
 LV_Modify(1,"Select")
}
select(x,y){
 if !x
 return
 Gui,1:Default
 sb_settext("Current segment: " x,2)
 list:=segkeeper()
 loop,parse,list,|
 {
  if A_LoopField=%x%
  {
   found:=A_Index
   break
  }
 }
 if !found
 found:=1,x:=Gui("seg")
 SendMessage,0x1330,% found-1,,SysTabControl322,%A_ScriptName%
 Sleep 0
 SendMessage,0x130C,% found-1,,SysTabControl322,%A_ScriptName%
 currentsegment(x)
 scintilla(gui(1),2358,"",winnumber(x))
 setpos(),file:=currentproject()
 SplitPath,file,,dir
 iniwrite,%x%,%dir%\options.ini,lastopen,segment
 ControlFocus,Scintilla1,%A_ScriptName%
 lastseg(currentproject(),x)
}
lastseg(x="",y=""){
 static seg:=[]
 if !y
 return seg[x]
 seg[x]:=y
}
selseg(){
 if (a_guievent = "i" || a_guievent = "normal")
 {
  if LV_GetNext()=0
  LV_Modify(LV_GetCount(),"Select")
  sc:=gui(1)
  scintilla(sc,2358,"",winnumber(currentsegment()))
  sb_settext("Current segment: " get(2,1),2)
  winactivate,%a_scriptname%
  send,^{home}
  sleep,1
  setpos()
  scintilla(sc,2242,0,sendstring(sc,2276,33,"_" scintilla(sc,2154),"int"))
  scintilla(sc,2122,gui("indent"))
 }
}
sendstring(sc,num,wparam="",lparam="",wpvalue="AStr",lpvalue="AStr"){
 return DllCall("SendMessageA", "UInt", sc,"int",num,wpvalue,wparam,lpvalue,lparam)
}
set(sc){
 language=asm
 DllCall("SendMessageA", "UInt", sc,"int",4006,"int","","AStr",language)
 loop,7
 sendstring(sc,4005,a_index-1,keywords(a_index),"int")
}
setcolor(sc,number=""){
 background:=dcolors("background")
 loop,35
 {
  if number
  {
   if a_index-1 != number
   continue
  }
  Scintilla(sc,2051,a_index-1,dcolors(a_index-1))
  Scintilla(sc,2052,a_index-1,background)
 }
 lnb:=iniread("font","linenumbers",16711680)
 scintilla(sc,2052,33,lnb)
 Scintilla(sc,2242,1,0) 
 Scintilla(sc,2242,2,13)
 scintilla(sc,2290,1,lnb)
 Scintilla(sc,2291,1,lnb)
 loop,5
 scintilla(sc,2041,a_index,0xff0000) 
 Scintilla(sc,2069,0xFFFFFF)
 name:=gui("name") , background:=rgb(dcolors("background"))
 while,x:=split(gui("windows"),",",a_index),w1:=a_index
 {
  if a_index=1
  continue
  d(a_index)
  gui,%a_index%:font,% gui("style") " c" rgb(dcolors(5)),% gui("name")
  gui,%a_index%:color,%background%,%background%
  winget,controllist,controllist,%x%
  while,y:=split(controllist,"`n",a_index)
  {
   guicontrol,%w1%:+background%background%,%y%
   guicontrol,%w1%:font,%y%
  }
 }
}
setkeywords(){
 controlgettext,out,Edit1,Highlight text editor
 iniwrite("keywords",get(12),out)
}
setpos(x=0,y=0){
 sc:=gui(1)
 setpos:
 pos:=split(getpos(currentsegment()),",",1)
 if split(1)
 scintilla(sc,2160,pos,pos)
 scintilla(sc,2613,split(2))
}
settings(x=""){
 static
 gui,7:submit,nohide
 if x
 {
  out:=%x%
  return out
 }
 Window("7,Settings,,tab~w500 h500~Hotkeys|Ftp Settings|General Settings,listview~y+5 x+5 w484 h450 goptions altsubmit~Action|Key|Window,hotkey~ggh vhk h25,nexttab,text~section~Server information for upload,text~~FTP server address,edit~gserver x+10 w200~" iniread("ftp","server"," ") ",text~xs y+5~Username,edit~guser x+10 w200~" iniread("ftp","user"," ") ",text~xs y+5~Password,edit~gpass password x+10 w200~" iniread("ftp","pass"," ") ",nexttab,text~~Tab Indentation (numbers only),edit~x+10 w40 number vindent ggh~" iniread("indent","level",1) ",text~xs y+5~End Characters for Replacements,edit~x+10 w200 ggh vendrep~")
 list:=
 while,x:=split(sv("end"),",",a_index)
 list.=chr(x)
 cst("Edit5",list,"Settings")
 hotkey(2,gui("options"))
 d(7)
 loop,3
 lv_modifycol(a_index,"AutoHdr")
}
showprogram(){
 if a_guievent in normal,K
 {
  d(11)
  controlgettext,out,Button2,Plugins
  if out
  {
   filecreatedir,downloads
   filename:=get(11)
   urldownloadtofile,http://www.autohotkey.net/~maestrith/Script Writer/plugins/%filename%,downloads\%filename%
   cst("Edit1",read("downloads\" filename),"Plugins")
  }
  else
  cst("Edit1",read("plugins\" get(11)),"Plugins")
 }
}
size(title,get=""){
 static size:=[]
 if get
 return size[title]
 if (a_gui=1){
  controlmove,Scintilla1,,,(a_guiwidth),(a_guiheight-68)
  ControlMove,SysTabControl321,,,% A_GuiWidth
  ControlMove,SysTabControl322,,,% A_GuiWidth
 }
 if (a_gui=2 || a_gui=3)
 controlmove,SysListView321,,,%a_guiwidth%,%a_guiheight%
 if a_gui=4
 controlmove,Edit1,,,%a_guiwidth%,%a_guiheight%
 out=w%a_guiwidth% h%a_guiheight%
 size[title]:=out
}
sort(){
 controlget,sort,checked,,Button1,A
 controlgettext,find,Edit1,A 
 iniwrite("search","sort",sort)
 tv_delete()
 GuiControl,5:-Redraw,TV
 search(find,sort)
}
split(x,y=0,z=0){
 static
 if z
 {
  out:=object()
  loop,parse,x,%y%
  out[a_index]:=a_loopfield
  return out[z]
 }
 return out[x]
}
string(x,y){
 sc:=gui(1)
 loop,parse,y,|
 scintilla(sc,split(A_LoopField,",",1),split(2),split(3),split(4))
}
stringreplace(w,x,y="",z=""){
 stringreplace,out,w,%x%,%y%,%z%
 return out
}
sv(x,y=""){
 static
 if !isobject(var)
 var:=object()
 if !y
 return var[x]
 var[x]:=y
}
testing(){
 sc:=gui(1)
 scintilla(sc,2548,2)
 sendstring(sc,2540,3,"annotation","int")
 sleep,500
 scintilla(sc,2548,0)
 d(2)
 while,LV_GetText(out,A_Index){
  script:=scripts(out)
  loop,parse,script,`n
  if (RegExMatch(A_LoopField,"(.*\(.*\).*{)",fun) && InStr(A_LoopField," ")=0)
  function.=substr(fun,1,-1) " "
 }
 sort,function,D%A_Space%
 return Clipboard:=RegExReplace(function," ","`r`n")
 sc:=gui(1),d(2)
 while,LV_GetText(out,A_Index){
  script:=scripts(out)
  loop,parse,script,`n
  if (RegExMatch(A_LoopField,"(.*:)(.*)",fun) && InStr(A_LoopField," ")=0)
  if fun2=`r
  function.=substr(fun1,1,-1) " "
 }
 sort,function,D%A_Space%
 sendstring(sc,2100,0,function,"int"),d(1)
 return
}
test_rcm(){
 mousegetpos,,,out
 if out!=% gui("id")
 {
  send,{rbutton}
  return
 }
 rightclickmenu()
 return
}
togglevisible(x){
 winget,out,style,%x%
 if out = 0x84cf0000
 winshow,%x%
 else
 winhide,%x%
 controlfocus,Scintilla1,%a_scriptname%
 vis:=if out = 0x84cf0000 ? 1 : 0
 iniwrite("visible",x,vis)
}
treeview(x){
 d(5)
 sc:=gui(1)
 child:=tv_getselection()
 out:=TV_GetChild(child)
 tv_gettext(pos,out)
 out:=TV_GetNext(out)
 tv_gettext(seg,out)
 select(seg,2)
 pos2:=pos+strlen(x)
 sleep,100
 Scintilla(sc,2160,pos,pos2)
 controlfocus,SysTreeView321,Search
}
tv(){
 if !(a_guievent = "normal" or a_guievent = "I")
 return
 controlgettext,find,Edit1,A
 keywait,lbutton,U
 treeview(find)
}
undo(){
 send,^z
}
up(){
 gui,10:hide
 dir:=currentproject(1)
 list=version,info,uploaddir
 while,x:=split(list,",",a_index)
 %x%:=window(10,x)
 filedelete,%dir%\version.ini
 fileappend,%info%,%dir%\version.ini
 iniwrite,%uploaddir%,%dir%\options.ini,ftp,directory
 iniwrite,%version%,%dir%\options.ini,version,number
 publish(1)
 server:=iniread("ftp","server")
 user:=iniread("ftp","user")
 pass:=iniread("ftp","pass")
 uploaddir:=iniread("ftp","directory","",1)
 up=mkdir "%uploaddir%"
 filename:=currentproject(2)
 up:=!uploaddir?:up
 FileAppend,
 (
 open %server%`n%user%`n%pass%
 binary
 hash
 %up%
 put "%a_scriptdir%\ftpfile.ftp" "%uploaddir%/%filename%"
 put "%dir%\version.ini" "%uploaddir%/version.txt"
 ls -l
 quit
 ), FTPCommands.txt
 settimer,getpercentage,50
 RunWait %comspec% /c ftp.exe -s:"FTPCommands.txt" >"FTPLog.txt",,hide
 FileDelete FTPCommands.txt
 loop,read,FTPLog.txt
 {
  regexmatch(a_loopreadline,"You uploaded(.*)",out)
  if out
  {
   settimer,getpercentage,off
   progress,off
   msgbox %out%
   compile()
  }
 }
 return
}
update(x=0){
 static updated:=[]
 sc:=gui(1)
 d(2)
 if x=2
 return updated:=object()
 if x=1
 {
  for a,b in updated
  if (a && b)
  list.=a "~" b ","
  return list
 }
 z:=currentproject()
 SplitPath,z,,,,filename
 filename:=clean1(filename)
 out:=currentsegment()
 if scintilla(sc,2159){
  updated[out]:=filename
  scintilla(sc,2014)
 }
 ;if updated not contains %out%
 ;updated.=out ","
 d(1)
 scripts(currentsegment(),scintilla_gettext(sc))
 scintilla(sc,2242,0,sendstring(sc,2276,33,"_" scintilla(sc,2154),"int"))
}
upload(){
 if !iniread("ftp","server",0)
 {
  msgbox please setup your ftp server in the options.
  settings()
  return
 }
 newline=`r`n
 dir:=currentproject(1)
 iniread,version,%dir%\options.ini,version,number,0.0
 window("10,Version Information,,edit~w400 vversion~" ver(version) ",edit~w400 h500 vinfo~,text~~Upload directory,edit~w300 x+10 vuploaddir~~,button~x10 ggh vup~Upload your script")
 cst("Edit2",ver(version) newline newline read(dir "\version.ini"),"Version")
 controlfocus,Edit2,Version
 iniread,out,%dir%\options.ini,ftp,directory,Default
 cst("Edit3",out,"Version")
 send,^{home}{down}
 return
 server:
 user:
 pass:
 saveoption(a_thislabel)
 return
}
var(){
 d(3)
 lv_gettext(out,lv_getnext())
 d(1)
 return out
}
Variables(){
 togglevisible(a_thisfunc)
}
var_ddl(){
 sc:=gui(1)
 var:=IniRead("variables")
 while,x:=split(var,"`n",A_Index)
 vars.=split(x,"=",2) " "
 sendstring(sc,2100,0,vars,"int")
}
ver(v){
 v:=substr(v,1,instr(v,".",0,0))(substr(v,(instr(v,".",0,0)+1))+1)
 iniwrite,%v%,%dir%\options.ini,version,number
 return v
}
version(){
 urldownloadtofile,http://www.autohotkey.net/~maestrith/Script Writer/version.txt,update.html
 Window("9,Version=" gui("version") ",,edit~x0 y0 w500 h600")
 cst("Edit1",read("update.html"),"Version")
 send,^{home}
}
window(x="",y="",z=""){
 static
 tab=1
 if y
 {
  gui,%x%:submit,nohide
  out:=%y%
  return out
 }
 stringsplit,w,x,`,
 gui,%w1%:destroy
 gui,%w1%: +resize +owner1
 gui,%w1%:font,% gui("style") " c" rgb(dcolors(5)),% gui("name")
 gui,%w1%:color,%background%,%background%
 loop, %w0%
 {
  if a_index < 4
  continue
  stringsplit,f,w%a_index%,~
  if f1=nexttab 
  {
   tab++
   gui,%w1%:tab,%tab%
   continue
  }
  gui,%w1%:font,s%z%
  gui,%w1%:add,%f1%,%f2%,%f3%
  gui,%w1%:font,s%z%
 }
 gui,%w1%:show,%w3%,%w2%
 winget,controllist,controllist,%w2%
 background:=rgb(dcolors("background"))
 while,x:=split(controllist,"`n",a_index)
 guicontrol,%w1%:+background%background%,%x%
}
windowcolor(x,color,win){
 d(x)
 gui,%x%:font,% gui("style") " c" color,% iniread("font","name")
 gui,%x%:color,EEEEEE,%background%
 winget,controllist,controllist,%win%
 while,y:=split(controllist,"`n",a_index)
 {
  guicontrol,%x%:+background%background%,%y%
  guicontrol,%x%:font,%y%
 }
}
winnumber(x,y=0,z=0){
 static
 if !number
 number:=object(),name:=object()
 if !y
 return number[x]
 number[x]:=y
 name[y]:=x
 if z
 return name[x]
}
Word_wrap(){
 scintilla(gui(1),2268,options("&word wrap",1))
}
word_wrap_markers(){
 iniwrite("font",32,Dlg_Color(iniread("font",32,dcolors(32))))
 dcolors("reload")
 setcolor(gui(1))
 setcolor(gui("font_color"))
}
import(x,name){
 file:=x
 fileread,script,%file%
 loop,parse,script,`n,`r
 {
  if (RegExMatch(A_LoopField,"(.*\(.*\).*{)",fun) && substr(a_loopfield,0)="{")
  start=1
  if !start
  everything.=a_loopfield "`n"
  if start
  {
   stringsplit,out,a_loopfield,;
   out:=trim(out1)
   if SubStr(out,0) = "{"
   indent++
   if out = `}
   indent--
   program.=a_loopfield "`n"
   if indent=0
   {
    prognum++
    final%prognum%:=program
    program=
    start=
   }
  }
 }
 dir=%A_ScriptDir%\projects\%name%
 fileremovedir,%dir%,1
 filecreatedir,%dir%
 loop,%prognum%
 fileappend,% trim(final%a_index%,"`r`n"),% dir "\" substr(final%a_index%,1,instr(final%a_index%,"(")-1)
 loop,%dir%\*.
 list.=a_loopfilename ","
 fileappend,%everything%,%dir%\%name%
 include:=chr(35) "include"
 fileappend,%include% %name%`n,%dir%\%name%.ahk
 loop,parse,list,`,
 if a_loopfield
 fileappend,%include% %a_loopfield%`n,%dir%\%name%.ahk
 return
}
post_to_ahknet(){
 inputbox,nickname,Input your nickname,Your nickname?,,,,,,,,Me
 InputBox,announce,Would you like to announce this to IRC?,1 for yes 0 for no,,,,,,,,0
 InputBox,description,Description,Describe this script,,,,,,,,My Script
 Code	:=  publish("quiet")
 name:=UrlEncode(Code)
 POST := "text=" . name . "&irc=" . Announce . "&author=" . Nickname . "&description=" . Description . "&submit=submit"
 URL := "http://www.autohotkey.net/paste/"
 httpquery( r := "", URL, POST )
 VarSetCapacity(r, -1)
 returl_start := InStr(r, "<title>Paste #")
 returl_end := InStr(r, "</title>")
 returl := SubStr(r, returl_start + 14, returl_end - 14 - returl_start)
 ClipBoard := "http://www.autohotkey.net/paste/" . returl
 MsgBox % "<" . ClipBoard . ">"
 Return
}
URLEncode(URL)
{
 StringReplace, URL, URL, `%, `%25, All
 FormatInteger := A_FormatInteger, FoundPos := 0
 SetFormat, IntegerFast, Hex
 While, (FoundPos := RegExMatch(URL,"iS)[^\w-\.~%]",Char,FoundPos + 1))
 StringReplace, URL, URL, %Char%, % "%" . SubStr("0" . SubStr(Asc(Char),3),-1), All
 SetFormat, IntegerFast, %FormatInteger%
 Return, URL
}
httpQuery(byref Result, Url, POSTDATA="", HEADERS=""){
 WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
 WebRequest.Open("POST", Url)
 WebRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
 WebRequest.Send(POSTDATA)
 Result := WebRequest.ResponseText
 WebRequest := ""
}
currentproject(x=""){
 static
 static split:=[]
 if x=1
 return dir
 if x=2
 return filename
 if x=3
 return noext
 if !x
 return out
 out:=x
 SplitPath,out,filename,dir,,noext
}
projects(x=""){
 static projects:=[]
 if projects[x]{
  projects.remove(x),remove:=1
 }
 if (!x || remove)
 {
  for a,b in projects
  open.=a "|"
  return open
 }
 projects[x]:=x
}
currentsegment(x=""){
 static segment:=[]
 if !x
 return segment[currentproject()]
 segment[currentproject()]:=x
}
segkeeper(x="",y=""){
 static segments:=[]
 SplitPath,x,,,,x
 x:=clean(x)
 if !x
 {
  tab:=currentproject()
  splitpath,tab,,,,tab
  tab:=clean(tab)
  return segments[tab]
 }
 segments[x]:=y
}
close(x=""){
 GuiControl,,SysTabControl321,% "|" projects(currentproject())
 GuiControl,Choose,SysTabControl321,1
 SetTimer,project,1
}
lastvariable(project="",variable=""){
 static lastvariable:=[]
 if !variable
 return lastvariable[project]
 lastvariable[project]:=variable
}
